//
//  HWGameDetailDownloadView.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/15.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameDetailDownloadView: UIView {
    var evaluateImageView:UIImageView!
    var evaluateLabel:UILabel!
    var evaluateButton:UIButton!
    var collectionImageView:UIImageView!
    var collectionLabel:UILabel!
    var collectionButton:UIButton!
    var downloadButton:UIButton!
    var model:HWGameDetailModel?{
        didSet {
            if model?.shoucang == 1 {
                collectionLabel.text = "已收藏"
                collectionLabel.textColor = .colorWithHexString(hexString: "#FEAC0A")
                collectionImageView.image = UIImage(named: "collectionImage_selected")
            } else {
                collectionLabel.text = "收藏"
                collectionLabel.textColor = .colorWithHexString(hexString: "#999999")
                collectionImageView.image = UIImage(named: "collectionImage_normal")
            }
            
            downloadButton.isHidden = false
            downloadButton.setBackgroundImage(UIImage(named: "downloadBtnBg"), for: .normal)
            if model?.system_type == 1 {
                if (model?.ios.count ?? 0) > 0 {
                    if (model?.ios_bundle.count ?? 0) > 0 {
                        if model?.haveBundle == true {
                            downloadButton.setTitle("立即打开", for: .normal)
                        }else {
                            downloadButton.isHidden = true
                            downloadButton.setTitle("立即下载", for: .normal)
                        }
                        
                    }else{
                        downloadButton.isHidden = true
                        downloadButton.setTitle("立即下载", for: .normal)
                    }
                } else {
                    downloadButton.setBackgroundImage(UIImage(named: "downloadBtnGrayBg"), for: .normal)
                    downloadButton.setTitle("未上线", for: .normal)
                }
                
            }else {
                downloadButton.setTitle("打开", for: .normal)
            }
        }
    }
    
    var evaluateButtonClick:(()->())?
    var collectionButtonClick:(()->())?
    var downLoadButtonClick:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameDetailDownloadView {
    private func initUI() -> Void {
        backgroundColor = .white
        layer.shadowColor = UIColor.colorWithHexString(hexString: "#D1D1D1").cgColor
        layer.shadowOffset = CGSize(width: 0, height: 0)
        layer.shadowRadius = 6*widthScale
        layer.shadowOpacity = 1
        
        evaluateImageView = UIImageView()
        evaluateImageView.image = UIImage(named: "evaluateImage")
        addSubview(evaluateImageView)
        
        evaluateLabel = HWLabel.createLabel(title: "评价", color: "#999999", font: 12*widthScale)
        addSubview(evaluateLabel)
        
        evaluateButton = UIButton(type: .custom)
        evaluateButton.addTarget(self, action: #selector(evaluateButtonClick(_:)), for: .touchUpInside)
        addSubview(evaluateButton)
        
        
        collectionImageView = UIImageView()
        collectionImageView.image = UIImage(named: "collectionImage_normal")
        addSubview(collectionImageView)
        
        collectionLabel = HWLabel.createLabel(title: "收藏", color: "#999999", font: 12*widthScale)
        addSubview(collectionLabel)
        
        collectionButton = UIButton(type: .custom)
        collectionButton.addTarget(self, action: #selector(collectionButtonClick(_:)), for: .touchUpInside)
        addSubview(collectionButton)
        
        downloadButton = UIButton(type: .custom)
        downloadButton.frame = CGRect(x: 0, y: 0, width: 248*widthScale, height: 36*widthScale)
        downloadButton.layer.cornerRadius = 4*widthScale
        downloadButton.setTitle("立即下载", for: .normal)
        downloadButton.setTitleColor(.white, for: .normal)
        downloadButton.titleLabel?.font = .boldSystemFont(ofSize: 14*widthScale)
        downloadButton.setBackgroundImage(UIImage(named: "downloadBtnBg"), for: .normal)
        downloadButton.layer.masksToBounds = true
        downloadButton.addTarget(self, action: #selector(downLoadButtonClick(_:)), for: .touchUpInside)
        addSubview(downloadButton)
    }
    
    private func initFrame() -> Void {
        evaluateImageView.snp.makeConstraints { make in
            make.left.equalTo(22*widthScale)
            make.top.equalTo(10*widthScale)
            make.width.height.equalTo(20*widthScale)
        }
        
        evaluateLabel.snp.makeConstraints { make in
            make.centerX.equalTo(evaluateImageView)
            make.top.equalTo(evaluateImageView.snp.bottom).offset(2*widthScale)
            if screenHeight >= 812 {
                make.bottom.equalTo(-45*widthScale)
            }else {
                make.bottom.equalTo(-11*widthScale)
            }
        }
        
        evaluateButton.snp.makeConstraints { make in
            make.left.top.right.equalTo(evaluateImageView)
            make.bottom.equalTo(evaluateLabel)
        }
        
        collectionImageView.snp.makeConstraints { make in
            make.left.equalTo(evaluateImageView.snp.right).offset(27*widthScale)
            make.top.equalTo(evaluateImageView)
            make.width.height.equalTo(20*widthScale)
        }
        
        collectionLabel.snp.makeConstraints { make in
            make.centerX.equalTo(collectionImageView)
            make.top.equalTo(collectionImageView.snp.bottom).offset(2*widthScale)
        }
        
        collectionButton.snp.makeConstraints { make in
            make.left.top.right.equalTo(collectionImageView)
            make.bottom.equalTo(collectionLabel)
        }
        
        downloadButton.snp.makeConstraints { make in
            make.right.equalTo(-14*widthScale)
            make.top.equalTo(12*widthScale)
            make.width.equalTo(248*widthScale)
            make.height.equalTo(36*widthScale)
        }
        
    }
}
@objc
extension HWGameDetailDownloadView {
    func evaluateButtonClick(_:UIButton) -> Void {
        self.evaluateButtonClick?()
    }
    func collectionButtonClick(_:UIButton) -> Void {
        self.collectionButtonClick?()
    }
    func downLoadButtonClick(_:UIButton) -> Void {
        self.downLoadButtonClick?()
    }
}
